package core.resources
{
import core.model.GameModel;
import core.service.Service;
import core.service.ServiceRequest;
import core.service.service_ns;

import flash.events.Event;

import utils.TimeUtil;

/**
 * GameDataResourceTask
 * @author jliang
 *
 */
public class GameDataResourceTask extends LoadTaskBase
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function GameDataResourceTask()
	{
		super();
	}
	
	override public function start():void
	{
		Service.service_ns::initialize();
		
		Service.invoke(new ServiceRequest("system_login", null, onLoginHandler));
	}
	
	private function onLoginHandler(data:Object):void
	{
		TimeUtil.startGameTime(data.timestamp);
        
        params.isNewPlayer =(int(data.is_new) == 1 ? true : false);
		
		GameModel.instance.initialize();
        
        if (params.isNewPlayer == true)
        {
            this.complete();
            return;
        }
        
        var loader:PlayerDataResourceTask = new PlayerDataResourceTask();
        loader.params = this.params;
        loader.addEventListener(Event.COMPLETE, playerDataReadyHandler);
        loader.start();
	}
	
	private function playerDataReadyHandler(event:Event):void
	{
        var loader:PlayerDataResourceTask = event.target as PlayerDataResourceTask;
        loader.removeEventListener(Event.COMPLETE, playerDataReadyHandler);
		
		this.complete();
	}
}
}



